package txx;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Scale;
import org.eclipse.swt.widgets.Shell;

/**
 * 
 * 图形学实验，简单光照模型
 */
public class TxxPhong {

    public static void main(String[] args) {
        new TxxPhong().run();
    }
    private Canvas canvas = null;
    private double kd = 0.2;
    private double ks = 0.8;
    private int n = 10;
    // 颜色： 0 红色，1绿色，2蓝色
    private int color = 2;
    private int light_tb = -1;
    private int light_lr = -1;
    
    private void run() {
        final Display display = Display.getDefault();
        final Shell shell = new Shell(SWT.MIN);
        shell.setText("简单光照模型实验");
        shell.setSize(680, 440);
        shell.setLayout(new FillLayout());
        
        Composite panel = new Composite(shell, SWT.NONE);
        panel.setLayout(new RowLayout(SWT.HORIZONTAL));
        Composite drawArea = new Composite(panel, SWT.NONE);
        drawArea.setLayoutData(new RowData(500, 400));
        drawArea.setLayout(new FillLayout());
        canvas = new Canvas(drawArea, SWT.BORDER);
        // 背景色
        final Color bgColor = new Color(Display.getDefault(), 189, 238, 247);
        // 监听canvas的重绘事件
        canvas.addPaintListener(new PaintListener() {

            public void paintControl(final PaintEvent event) {
                GC gc = event.gc;
                // 用环境光填充背景
                gc.setBackground(bgColor);
                gc.fillRectangle(0, 0, canvas.getSize().x, canvas.getSize().y);
                drawOne(gc);
            }
        });

        Composite controlPanel = new Composite(panel, SWT.NONE);
        controlPanel.setLayoutData(new RowData(160, 400));
        controlPanel.setLayout(new RowLayout());
        
        Composite colorPanel = new Composite(controlPanel, SWT.NONE);
        colorPanel.setLayoutData(new RowData(160, 22));
        colorPanel.setLayout(new RowLayout());
        Label lc = new Label(colorPanel, SWT.NONE);
        lc.setText("球体颜色：");
        Combo combo = new Combo(colorPanel, SWT.READ_ONLY);
        combo.add("红色(R)");
        combo.add("绿色(G)");
        combo.add("蓝色(B)");
        combo.setText("蓝色(B)");
        combo.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                Combo combo = (Combo)e.getSource();
                color = combo.getSelectionIndex();
                canvas.redraw();
            }
        });
        
        Composite lightPanel = new Composite(controlPanel, SWT.NONE);
        lightPanel.setLayoutData(new RowData(160, 30));
        lightPanel.setLayout(new RowLayout());
        Label llight1 = new Label(lightPanel, SWT.NONE);
        llight1.setText("光照方向：");
        Combo comLR = new Combo(lightPanel, SWT.READ_ONLY);
        comLR.add("左");
        comLR.add("右");
        comLR.setText("右");
        comLR.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                Combo combo = (Combo)e.getSource();
                System.out.println(combo.getSelectionIndex());
                light_lr = combo.getSelectionIndex() == 0 ? 1 : -1;
                canvas.redraw();
            }
        });
        
        Combo comTB = new Combo(lightPanel, SWT.READ_ONLY);
        comTB.add("上");
        comTB.add("下");
        comTB.setText("下");
        comTB.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                Combo combo = (Combo)e.getSource();
                light_tb = combo.getSelectionIndex() == 0 ? 1 : -1;
                canvas.redraw();
            }
        });
        
        Label ln = new Label(controlPanel, SWT.NONE);
        ln.setText("镜面反射指数n（1~100）");
        final Scale sn = new Scale(controlPanel, SWT.NONE);
        sn.setMinimum(1);
        sn.setMaximum(100);
        sn.setIncrement(10);
        sn.setPageIncrement(10);
        sn.setSelection(5);
        sn.addMouseListener(new MouseAdapter() {
            public void mouseUp(MouseEvent e) {
                n = sn.getSelection();
                canvas.redraw();
            }
        });
        
        Label lkd = new Label(controlPanel, SWT.NONE);
        lkd.setText("漫反射系数Kd（0.1~1）");
        final Scale slider = new Scale(controlPanel, SWT.NONE);
        slider.setMinimum(10);
        slider.setMaximum(100);
        slider.setIncrement(10);
        slider.setPageIncrement(10);
        slider.setSelection(20);
        slider.addMouseListener(new MouseAdapter() {
            public void mouseUp(MouseEvent e) {
                kd = slider.getSelection() / 100.0;
                canvas.redraw();
            }
        });
        Label lks = new Label(controlPanel, SWT.NONE);
        lks.setText("镜面反射系数Ks（0.1~1）");
        final Scale sks = new Scale(controlPanel, SWT.NONE);
        sks.setMinimum(10);
        sks.setMaximum(100);
        sks.setIncrement(10);
        sks.setPageIncrement(10);
        sks.setSelection(80);
        sks.addMouseListener(new MouseAdapter() {
            public void mouseUp(MouseEvent e) {
                ks = sks.getSelection() / 100.0;
                canvas.redraw();
            }
        });
        
        shell.layout();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
    
    private void drawOne(GC gc) {
        double[] Kds = new double[3];
        double[] Kss = new double[3];
        Kds[color] = kd;
        Kss[color] = ks;
        Phong phong = new Phong(gc, 0, 0, 500, 400, 100, Kds, Kss, n, new int[]{light_lr, light_tb, 1});
        phong.drawPhong();
    }
    
    
    private void drawSome(GC gc) {
        Color textColor = new Color(Display.getDefault(), 255, 0, 0);
        // 镜面反射指数
        int[] an = new int[] { 5, 10, 20, 40 };
        // 漫反射系数
        double[] kds = new double[] { 0.8, 0.6, 0.4, 0.2, 0 };
        // 镜面反射系数
        double[] kss = new double[] { 0.2, 0.4, 0.6, 0.8, 1 };
        for (int j = 0; j < 4; j++) {
            for (int i = 0; i < 6; i++) {
                if (i == 5) { // 写说明，镜面反射指数
                    gc.setForeground(textColor);
                    gc.drawText("n=" + an[j], i * 90, j * 90 + 45);
                    continue;
                }
                Phong phong = new Phong(gc, i * 90, j * 90, kds[i], kss[i], an[j]);
                phong.drawPhong();
            }
        }

        // 写说明， 漫反射和镜面反射系数
        for (int i = 0; i < 6; i++) {
            if (i == 5) {
                gc.setForeground(textColor);
                gc.drawText("Kd/Ks", i * 90, 4 * 90);
                continue;
            }
            gc.setForeground(textColor);
            gc.drawText(kds[i] + "/" + kss[i], i * 90 + 25, 4 * 90);
        }
    }

}
